%% Performance Evaluation of EW UEP RLNC Coding Scheme
%
% Author: 12GR652
% Last modified: 26-04-2012
%
% Description:
% This script drives the ewrlnc simulation and saves results in a .mat
% for later plotting
%
% Speed:
%   - Note that ewrlnc(... ) is very very slow - compute with power!
%

%% Startup

% Clear workspace
% close all
% clear all
% clc

% Setup matlab worker threads for parallel execution
% Utilizing 8 cores on js2.es.aau.dk crashed... trying 4 instead...

target_nb_worker_threads=2;
nb_worker_threads=matlabpool('size');

if nb_worker_threads==0
    matlabpool('open',target_nb_worker_threads)
    nb_worker_threads=matlabpool('size');
else 
    matlabpool('close','force')
    matlabpool('open',target_nb_worker_threads)
    nb_worker_threads=matlabpool('size');
end
    
% Note time (performance)
tic

% Prepare logfile
the_time_now=datestr(now);
filename_log=strcat(the_time_now,'_ewrlnc_log.txt');
filename_fig=strcat(the_time_now,'_ewrlnc_log.eps');
diary(filename_log)
diary on

% Begin diary
disp('Testing Expanding Windows RLNC');
disp('Nb of worker threads');
disp(nb_worker_threads)


%% Parameters to test EWRLNC with:

m=8; % field parameter (2^m = field size), we shall use either 1 or 8
nb_sims=10; % simulations pr nb_transmissions
R1=0.5; % probability of layer 1
R2=0.25; % Probability of layer 2
R3=0.25; % probability of layyer 3

I_pkts=3; % Nb of packets in I block
P1_pkts=3;% Nb of packets in P1 block
P2_pkts=3; % Nb of packkets in P2 block
gen_size=I_pkts+P1_pkts+P2_pkts; % Generation size

%% Save configuration to logfile

disp('Field parameter (2^m = field size), we shall use either 1 or 8');
disp(m);
disp('Simulations pr nb_transmissions');
disp(nb_sims);
disp('Probability of transmitting layer 1');
disp(R1);
disp('Probability of transmitting layer 2');
disp(R2);
disp('Probability of transmitting layer 3');
disp(R3);
disp('Nb of packets in I block');
disp(I_pkts);
disp('Nb of packets in P1 block');
disp(P1_pkts);
disp('Nb of packets in P2 block');
disp(P2_pkts);
disp('Generation size');
disp(gen_size);

%% Collect data

I_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding I
P1_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding P1
P2_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding P2

% For pkt_loss=0 and increasing number of packets send, collect data
% Send until 3 x generation size has been send
parfor nb_transmissions = 1:(3*gen_size)
    [I,P1,P2]=ewrlnc(nb_sims,I_pkts,P1_pkts,P2_pkts,m,nb_transmissions,R1,R2,R3);
    I_prob(nb_transmissions)=I/nb_sims;
    P1_prob(nb_transmissions)=P1/nb_sims;
    P2_prob(nb_transmissions)=P2/nb_sims;
end

% Evaluate for EEP (we can tweak our UEP to EEP, by R1=0, R2=0, R3=1)

% EEP_prob=zeros(3*gen_size,1); % vector which will hold probability of decoding P2(all pkts)
% 
% parfor nb_transmissions = 1:(3*gen_size)
%     [I,P1,P2]=ewrlnc(nb_sims,I_pkts,P1_pkts,P2_pkts,m,nb_transmissions,0,0,1);
%     EEP_prob(nb_transmissions)=P2/nb_sims;
% end

% All data has been generated, however another script will handle the
% plotting, save matlab workspace for now.

% Save all data for plotter
save('ewrlnc_data'); 

% Finish data generation
disp('It took [s]')
disp(toc)
disp('This is the end...')
diary off

matlabpool('close','force') % close thread pool